import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

typedef SwitchCallback<bool> = void Function(bool);
typedef ClickActionCallback = void Function();

class CustomCell extends StatefulWidget {
  CustomCell({
    super.key,
    @required this.title,
    this.subTitle,
    this.imageName,
    this.subImageName,
    this.isSwitch = false,
    this.isHideArrows = false,
    this.switchCallback,
    this.rowHeight,
    this.clickAction,
    this.fontSize,
    this.fontWeight,
    this.fontColor,
    this.switchValue,
    this.disable = false,
    this.borderRadius = 0,
    this.hasNewVersion = false,
    this.isRequired = false,
    this.subTitleColor = null,
    this.subTitleFontSize,
    this.titleFontSize,
    this.backgroundColor = Colors.white,
    this.subWidget

  }) : assert(title != null, 'title 不能为空！');

  final String? title;
  final String? subTitle;
  final String? imageName;
  final String? subImageName;
  final bool? isSwitch;
  final bool? switchValue;
  final SwitchCallback<bool>? switchCallback;
  final double? rowHeight;
  final ClickActionCallback? clickAction;

  final double? fontSize;
  final Color? fontColor;
  final bool? isHideArrows;
  final bool? disable;
  final double borderRadius;
  final bool? hasNewVersion;
  final bool? isRequired;
  final Color? subTitleColor;//副标题 有值 颜色
  final double? subTitleFontSize;
  final double? titleFontSize;
  final Color backgroundColor;//背景颜色
  final Widget? subWidget;//背景颜色
  final FontWeight? fontWeight;


  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    print('1111 ========= disable: ${disable}');

    return _CustomCellState(
      fontSize: fontSize,
      fontColor: fontColor,
      switchValue: switchValue,
      isHideArrows: isHideArrows,
      subTitleColor: subTitleColor,
      backgroundColor: backgroundColor
    );
  }
}

class _CustomCellState extends State<CustomCell> {
  _CustomCellState({
    this.fontSize,
    this.fontColor,
    this.switchValue,
    this.isHideArrows = false,
    this.subTitleColor = null,
    this.backgroundColor = Colors.white
  });

  final double? fontSize;
  final Color? fontColor;
  final bool? isHideArrows;
  final Color? subTitleColor;//副标题 颜色
  final Color backgroundColor;//背景颜色


  Color _currentColor = Colors.white;
  bool? switchValue;
  Color _selectColor = Colors.white;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _currentColor = backgroundColor;
    _selectColor = backgroundColor;
  }

  @override
  Widget build(BuildContext context) {

    return GestureDetector(
      onTap: () {
        print('------onTap');
        setState(() {
          _currentColor = backgroundColor;
        });
        widget.clickAction!();
      },
      onTapDown: (TapDownDetails details) {
        print('点击下来');
        setState(() {
          _currentColor = _selectColor;
        });
      },
      onTapCancel: () {
        print('点击 离开');
        setState(() {
          _currentColor = _selectColor;
        });
      },
      child: Container(
        height: widget.rowHeight ?? 56.h,
        decoration: BoxDecoration(
          color: widget.isSwitch! ? backgroundColor : _currentColor,
          borderRadius: BorderRadius.circular(widget.borderRadius),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            //left

            Expanded(child: Container(
              padding: EdgeInsets.only(left: 20.w),
              child: Row(
                children: [
                  //图标
                  widget.imageName?.isNotEmpty == true
                      ? Image(
                    image: AssetImage(widget.imageName!),
                    width: 28.w,
                  )
                      : SizedBox.shrink(),
                  widget.imageName?.isNotEmpty == true
                      ? SizedBox(width: 8.w)
                      : SizedBox.shrink(),
                  //Title
                  Flexible(
                    child: Text(
                      widget.title!,
                      maxLines: 2,
                      style: TextStyle(
                        letterSpacing: 0,
                        height: 1.2,
                        fontSize: widget.titleFontSize ?? 16.sp,
                        color:widget.fontColor != null ? widget.fontColor : Color(0xff1B1B1B),
                        fontWeight: widget.fontWeight ?? FontWeight.w400,
                        wordSpacing:0,
                      ),
                    ),
                  ),
                  Visibility(
                      visible: widget.isRequired!,
                      child: Text(
                        '*',
                        style: TextStyle(color: Color(0xffCB321B),fontSize: 16.sp,fontWeight: FontWeight.w400),
                      )
                  ),
                  Visibility(
                    visible: widget.hasNewVersion!,
                    child: Container(
                        alignment: Alignment.topLeft,
                        margin: EdgeInsets.only(top: 8.h,left: 5.w),
                        child: Container(
                          padding: EdgeInsets.all(2),
                          decoration: BoxDecoration(
                            color: Color(0xffED6D20),
                            borderRadius: BorderRadius.all(
                                Radius.circular(2)),
                          ),
                          child: Text(
                            'New',
                            style: TextStyle(fontSize: 8.sp,fontWeight: FontWeight.w600,color: Colors.white,wordSpacing: 0,height: 0),
                          ),
                        )
                    ),
                  ),
                ],
              ),
            ),),

            //right
            Container(
              margin: EdgeInsets.only(left: 16.w, right: 20.w),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  //subTitle
                  widget.subTitle != null
                      ? Container(
                    alignment: Alignment.centerRight,
                    child: Text(
                      widget.subTitle!,
                      style: TextStyle(
                        fontSize: widget.subTitleFontSize ?? 14.sp,
                        color: widget.disable! ? Color(0xffC3C3C3) : (widget.subTitleColor ?? Color(0xff5A5A5A)),

                      ),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                  )
                      : Text(''),
                  widget.subWidget != null
                      ? widget.subWidget!
                      : Container(),
                  //subImage
                  widget.subImageName != null
                      ? Image.asset(
                    widget.subImageName!,
                    width: 20.w,
                    height: 20.w,
                  )
                      : Container(),
                  //箭头
                  // Image(image:AssetImage('images/arrows.png'),width: 15.w,height: 15.h,),
                  widget.isSwitch!
                      ? Container(
                    // color: Colors.red,
                      height: 30.h,
                      width: 40.w,
                      child: FittedBox(
                        fit: BoxFit.fill,
                        child: Switch(
                          value: switchValue ?? false,
                          // 初始状态为开
                          // splashRadius: 12.w,
                          activeColor: Colors.white,
                          activeTrackColor: Color(0xffED6D20),
                          inactiveTrackColor: Color(0xffDADADA),
                          inactiveThumbColor: Colors.white,
                          trackOutlineWidth: MaterialStateProperty.all(0),
                          trackOutlineColor:MaterialStateProperty.all(Color(0xffDADADA)),
                          onChanged: (bool value) {
                            // 这里处理开关状态改变时的逻辑
                            setState(() {
                              switchValue = value;
                            });
                            widget.switchCallback!(value);
                          },
                        ),
                      ))
                      : Visibility(
                      visible: !isHideArrows!,
                      child: Image(
                        image:  AssetImage(widget.disable! ? 'images/arrows_disable.png' : 'images/arrows.png'),
                        width: 15.w,
                        height: 15.h,
                      )),
                  // widget.isSwitch! ? Switch(
                  //   value: switchValue ?? false, // 初始状态为开
                  //   activeColor: Colors.white,
                  //   activeTrackColor:Color(0xffED6D20),
                  //   inactiveTrackColor:Colors.grey,
                  //   inactiveThumbColor: Colors.white,
                  //   onChanged: (bool value) {
                  //     // 这里处理开关状态改变时的逻辑
                  //     setState(() {
                  //       switchValue =  value;
                  //     });
                  //     widget.switchCallback!(value);
                  //   },
                  // ): Image(image:AssetImage('images/arrows.png'),width: 15.w,height: 15.h,),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
